import 'package:FlutterDemo/utils/nav_utils.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';

import 'datas_blocs.dart';
import 'sample_flutterbloc2_details.dart';

/// https://pub.flutter-io.cn/packages/flutter_bloc/versions/6.1.3/example 官方示例
///
/// A [StatelessWidget] which uses:
//  * [bloc](https://pub.flutter-io.cn/packages/bloc)
//  * [flutter_bloc](https://pub.flutter-io.cn/packages/flutter_bloc)
class SampleFlutterBloc2 extends StatelessWidget {
  const SampleFlutterBloc2({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return BlocProvider(
        create: (context) => ThemeCubit(),
        child: BlocBuilder<ThemeCubit, ThemeData>(
          builder: (_, theme) {
            return MaterialApp(
              title: "Flutter Demo",
              home: BlocProvider(
                create: (context) => MyCounterBloc2(),
                child: CounterPage(),
              ),
              theme: theme,
            );
          },
        ));
  }
}

class CounterPage extends StatelessWidget {
  const CounterPage({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("Counter"),
      ),
      body: BlocBuilder<MyCounterBloc2, int>(
        builder: (_, count) {
          return Text(
            "$count",
            style: Theme.of(context).textTheme.headline1,
          );
        },
      ),
      floatingActionButton: Column(
        crossAxisAlignment: CrossAxisAlignment.end,
        mainAxisAlignment: MainAxisAlignment.end,
        children: [
          Padding(
            padding: EdgeInsets.symmetric(vertical: 5.0),
            child: FloatingActionButton(
              child: Icon(Icons.add),
              onPressed: () {
                // 计数器加法
                context.read<MyCounterBloc2>().add(MyCounterEvent2.increment);
              },
            ),
          ),
          Padding(
            padding: EdgeInsets.symmetric(vertical: 5.0),
            child: FloatingActionButton(
              child: Icon(Icons.remove),
              onPressed: () {
                // 计数器减法
                context.read<MyCounterBloc2>().add(MyCounterEvent2.decrement);
              },
            ),
          ),
          Padding(
            padding: EdgeInsets.symmetric(vertical: 5.0),
            child: FloatingActionButton(
              child: Icon(Icons.brightness_1),
              onPressed: () {
                //修改主题
                context.read<ThemeCubit>().toogleTheme();
              },
            ),
          ),
          Padding(
            padding: EdgeInsets.symmetric(vertical: 5.0),
            child: FloatingActionButton(
              backgroundColor: Colors.red,
              child: Icon(Icons.error),
              onPressed: () {
                // 计数器减法
                context.read<MyCounterBloc2>().add(null);
              },
            ),
          ),
          Padding(
            padding: EdgeInsets.symmetric(vertical: 5.0),
            child: FloatingActionButton(
              child: Icon(Icons.forward),
              onPressed: () {
                // 计数器减法
                nav2page(context, FlutterBloc2Details());
              },
            ),
          )
        ],
      ),
    );
  }
}
